
//////////////////////////////////////////////////////////////////////////
#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include "../l_neo_lexer/loc.h"
#include "../l_neo/mars.h"
#include "../l_service/c_ds/rmem.h"
#include "../l_service/c_ds/root.h"

void warning(Loc loc, const char *format, ...);
void error(Loc loc, const char *format, ...);
void verror(Loc loc, const char *format, va_list);
void fatal();
void halt();


extern Global global;

/**************************************
* Print error message and exit.
*/

void error(Loc loc, const char *format, ...)
{
	va_list ap;
	va_start(ap, format);
	verror(loc, format, ap);
	va_end( ap );
}

void warning(Loc loc, const char *format, ...)
{
	if (global.params.warnings && !global.gag)
	{
		fprintf(stdmsg, "warning - ");
		va_list ap;
		va_start(ap, format);
		verror(loc, format, ap);
		va_end( ap );
	}
}

void verror(Loc loc, const char *format, va_list ap)
{
	if (!global.gag)
	{
		char *p = loc.toChars();

		if (*p)
			fprintf(stdmsg, "%s: ", p);
		mem.free(p);

		fprintf(stdmsg, "Error: ");
#if _MSC_VER
		// MS doesn't recognize %zu format
		OutBuffer tmp;
		tmp.vprintf(format, ap);
		fprintf(stdmsg, "%s", tmp.oToChars());
#else
		vfprintf(stdmsg, format, ap);
#endif
		fprintf(stdmsg, "\n");
		fflush(stdmsg);
		//halt();
	}
	global.errors++;
}

/***************************************
* Call this after printing out fatal error messages to clean up and exit
* the compiler.
*/

void fatal()
{
	//@
	getchar();
#if 0
	halt();
#endif
	exit(EXIT_FAILURE);
}

/**************************************
* Try to stop forgetting to remove the breakpoints from
* release builds.
*/
void halt()
{
#ifdef DEBUG
	*(char*)0=0;
#endif
}

